/**
 * 剑指 Offer 06. 从尾到头打印链表
 * https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 */
const { displayBaseArray, createListNode } = require('../../libs/public');

/**
 * @param {ListNode} head
 * @return {number[]}
 */
function reversePrint(head) {
  let length = 0;
  let node = head;
  while (node) {
    length += 1;
    node = node.next;
  }
  const result = new Array(length).fill(0);
  let i = length - 1;
  node = head;
  while (node) {
    result[i] = node.val;
    i -= 1;
    node = node.next;
  }
  return result;
}

console.log(displayBaseArray(reversePrint(createListNode([1, 3, 2]))) === '[2,3,1]');
